package com.chushouya.controller.common;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.common.wx.dto.WxSchemeRequest;
import com.chushouya.common.wx.dto.WxSchemeResponse;
import com.chushouya.manager.service.common.WxMiniProgramService;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 微信小程序相关接口
 * 
 * @author shouboshi
 * @date 2025-01-23
 */
@Slf4j
@RestController
@RequestMapping(ServiceType.COMMON + "/wx-miniprogram")
@Api(tags = ServiceType.COMMON + "微信小程序接口")
public class WxMiniProgramController {

    @Resource
    private WxMiniProgramService wxMiniProgramService;

    /**
     * 生成微信小程序 URL Scheme
     * 适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景
     * 
     * @param request URL Scheme 请求参数
     * @return 生成的 scheme 码
     */
    @PostMapping("/generateScheme")
    @ApiOperation(value = "生成微信小程序URL Scheme", notes = "适用于短信、邮件、外部网页等拉起小程序")
    public Response<WxSchemeResponse> generateScheme(
            @ApiParam(value = "小程序AppId", required = true)
            @RequestParam String appId,
            @RequestBody WxSchemeRequest request) {
        try {
            log.info("生成微信小程序URL Scheme - appId: {}, 请求参数: {}", appId, request);
            
            // 验证参数
            if (Strings.isBlank(appId)) {
                return Response.error("appId不能为空");
            }
            
            // 调用服务生成 scheme
            WxSchemeResponse response = wxMiniProgramService.generateScheme(appId, request);
            
            log.info("生成微信小程序URL Scheme成功 - appId: {}, scheme: {}", appId, response.getOpenlink());
            return Response.success(response);
            
        } catch (Exception e) {
            log.error("生成微信小程序URL Scheme异常 - appId: {}, error: {}", appId, e.getMessage(), e);
            return Response.error("生成URL Scheme异常: " + e.getMessage());
        }
    }

    /**
     * 生成永久有效的微信小程序 URL Scheme（简化版）
     * 
     * @param path 小程序页面路径
     * @param query 页面参数
     * @return 生成的 scheme 码
     */
    @GetMapping("/generateScheme")
    @ApiOperation(value = "生成永久有效的URL Scheme（简化版）", notes = "快速生成永久有效的scheme码")
    public Response<String> generateSchemeSimple(
            @ApiParam(value = "小程序AppId", required = true)
            @RequestParam String appId,
            @ApiParam(value = "小程序页面路径", example = "pages/index/index")
            @RequestParam(required = false) String path,
            @ApiParam(value = "页面参数", example = "id=123&type=detail")
            @RequestParam(required = false) String query) {
        try {
            log.info("生成永久有效的微信小程序URL Scheme - appId: {}, path: {}, query: {}", appId, path, query);
            
            // 验证参数
            if (Strings.isBlank(appId)) {
                return Response.error("appId不能为空");
            }
            
            // 调用服务生成永久有效的 scheme
            String scheme = wxMiniProgramService.generateSchemeSimple(appId, path, query);
            
            log.info("生成永久有效的微信小程序URL Scheme成功 - appId: {}, scheme: {}", appId, scheme);
            return Response.success(scheme);
            
        } catch (Exception e) {
            log.error("生成永久有效的微信小程序URL Scheme异常 - appId: {}, error: {}", appId, e.getMessage(), e);
            return Response.error("生成URL Scheme异常: " + e.getMessage());
        }
    }
}
